/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIChamadosLista.java
 *
 * Created on 12/06/2011, 10:04:44
 */

package gui;

import dao.DAOChamadoCab;
import dao.ResultSetTableModel;
import javax.swing.JOptionPane;
import model.ChamadoCab;
import model.Utils;

/**
 * Interface que lista os chamados já abertos
 * @author Janio Luiz Antunes
 * @author Marcel Vinicius Kohls
 * @see http://code.google.com/p/sysdesk-java/
 *
 */
public class GUIChamadosLista extends javax.swing.JFrame {

    private DAOChamadoCab chamadoCabDAO = new DAOChamadoCab();
    private ChamadoCab chamadoCab = new ChamadoCab();
    private Utils util = new Utils();

    /** Creates new form GUIChamadosLista */
    public GUIChamadosLista() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbListaChamados = new javax.swing.JTable();
        jbtFechar = new javax.swing.JButton();
        jmnListaChamados = new javax.swing.JMenuBar();
        jmiDetalhes = new javax.swing.JMenu();
        jmiStatus = new javax.swing.JMenu();
        jmiFechado = new javax.swing.JMenuItem();
        jmiAberto = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lista de chamados");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jtbListaChamados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtbListaChamados);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 883, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                .addContainerGap())
        );

        jbtFechar.setText("Fechar");
        jbtFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtFecharActionPerformed(evt);
            }
        });

        jmiDetalhes.setText("Detalhes");
        jmiDetalhes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jmiDetalhesMouseClicked(evt);
            }
        });
        jmnListaChamados.add(jmiDetalhes);

        jmiStatus.setText("Status");
        jmiStatus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jmiStatusMouseClicked(evt);
            }
        });

        jmiFechado.setText("Trocar para FECHADO");
        jmiFechado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiFechadoActionPerformed(evt);
            }
        });
        jmiStatus.add(jmiFechado);

        jmiAberto.setText("Trocar para ABERTO");
        jmiAberto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAbertoActionPerformed(evt);
            }
        });
        jmiStatus.add(jmiAberto);

        jmnListaChamados.add(jmiStatus);

        setJMenuBar(jmnListaChamados);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(826, Short.MAX_VALUE)
                .addComponent(jbtFechar)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtFechar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Recebe uma instancia de DAOCHamadoCab para montar a lista de chamados no
     * grid.
     * @param pChamadoOrigem
     */
    public void setListaFiltrada(DAOChamadoCab pChamadoOrigem) {
        chamadoCabDAO = pChamadoOrigem;
    }

    /**
     *
     * Fecha esta janela
     */
    private void jbtFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtFecharActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtFecharActionPerformed

    /**
     *
     * Quando a janela estiver abrindo atualiza a tabela
     */
    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        atualizaTabela();
    }//GEN-LAST:event_formWindowGainedFocus

    private void jmiDetalhesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jmiDetalhesMouseClicked
        GUIChamadosDetalhes chamadosDetalhes = new GUIChamadosDetalhes();

        chamadosDetalhes.setLocation(util.getTelaColunaCentro(chamadosDetalhes), util.getTelaLinhaMaxima(chamadosDetalhes));
        chamadosDetalhes.setNomeCliente((String) jtbListaChamados.getValueAt(jtbListaChamados.getSelectedRow(), 5));
        chamadosDetalhes.setCodProtocolo( (Integer) jtbListaChamados.getValueAt(jtbListaChamados.getSelectedRow(), 0));

        chamadosDetalhes.setVisible(true);
    }//GEN-LAST:event_jmiDetalhesMouseClicked

    private void jmiStatusMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jmiStatusMouseClicked

    }//GEN-LAST:event_jmiStatusMouseClicked

    private void jmiFechadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiFechadoActionPerformed

        try {
            chamadoCabDAO.setStatus((Integer) jtbListaChamados.getValueAt(jtbListaChamados.getSelectedRow(), 0), chamadoCab.SIT_FECHADO);
            atualizaTabela();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro ao definir status: \n"+e.getMessage());
        }
    }//GEN-LAST:event_jmiFechadoActionPerformed

    private void jmiAbertoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiAbertoActionPerformed
        try {
            chamadoCabDAO.setStatus((Integer) jtbListaChamados.getValueAt(jtbListaChamados.getSelectedRow(), 0), chamadoCab.SIT_ABERTO);
            atualizaTabela();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro ao definir status: \n"+e.getMessage());
        }
    }//GEN-LAST:event_jmiAbertoActionPerformed


    /**
     * Monta uma tabela especifica baseada no banco de cabecalho de chamados
     */
    private void atualizaTabela(){
        try {
            String[] TitulosColunas = {"Protocolo", "Data", "Hora", "Categoria", "Aberto por", "Cliente", "Criticidade", "Status"};
            jtbListaChamados.setModel(new ResultSetTableModel(chamadoCabDAO.getListaCabecalho(true), TitulosColunas));
            jtbListaChamados.updateUI();
            jtbListaChamados.repaint();
        } catch (Exception e) {
            System.out.println("erro: "+e.getMessage());
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIChamadosLista().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtFechar;
    private javax.swing.JMenuItem jmiAberto;
    private javax.swing.JMenu jmiDetalhes;
    private javax.swing.JMenuItem jmiFechado;
    private javax.swing.JMenu jmiStatus;
    private javax.swing.JMenuBar jmnListaChamados;
    private javax.swing.JTable jtbListaChamados;
    // End of variables declaration//GEN-END:variables

}
